/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frame;

import java.awt.Color;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.Random;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JToggleButton;
import kbcquiz.App;
import library.DB;
import library.Utils;

public class Play extends javax.swing.JFrame {

    class ListPoint {

        int point;
        int minus;
        int walk;
        int wrong;

        public ListPoint(int point, int miss, int walk, int wrong) {
            this.point = point;
            this.minus = miss;
            this.walk = walk;
            this.wrong = wrong;
        }
    }
    /**
     Variable to make sorcelist select next question
     */
    int listSelectedIndex;
    /**
     Set to to enable timemod
     */
    boolean timeMode = false;
    String answer;
    Vector pointList = new Vector(13);
    /**
     Last clicked button
     */
    JToggleButton lastButton;
    /**
     right answer button index
     */
    int rightAnswerButtonIdx;
    /**
     * Flag using fifty-fifty
     */
    public boolean fifty = false;
    /**
     * Flag using flip
     */
    public boolean flip = false;
    /**
     * Flag using double dip
     */
    public boolean doubledip = false;
    public boolean usedDoubledip = false;
    ArrayList AnswerSelectList = new ArrayList(2);

    public String sessionID;
    
    public Play() {
        initComponents();
        
        jQuestion.setEditable(false);
        
        /*1*/pointList.add(new ListPoint(50, 0, 0, 0));
        /*2*/pointList.add(new ListPoint(100, 0, 50, 50));
        /*3*/pointList.add(new ListPoint(200, 100, 100, 100));
        /*4*/pointList.add(new ListPoint(400, 100, 200, 100));
        /*5*/pointList.add(new ListPoint(800, 100, 400, 300));
        /*6*/pointList.add(new ListPoint(1600, 100, 800, 700));
        /*7*/pointList.add(new ListPoint(3200, 100, 1600, 1500));
        /*8*/pointList.add(new ListPoint(6400, 3200, 3200, 3200));
        /*9*/pointList.add(new ListPoint(12500, 3200, 6400, 3200));
        /*10*/pointList.add(new ListPoint(25000, 3200, 12500, 9300));
        /*11*/pointList.add(new ListPoint(50000, 3200, 25000, 21800));
        /*12*/pointList.add(new ListPoint(100000, 3200, 50000, 46800));
        /*13*/pointList.add(new ListPoint(500000, 3200, 100000, 96800));

        Vector itemSocores = new Vector();

        for (int i = 0; i < pointList.size(); i++) {
            ListPoint a = (ListPoint) pointList.get(i);
            int pos = i + 1;
            itemSocores.add(pos + ". " + a.point + "$");
        }

        //Build a score list and display it
        this.PrizeMoney.setListData(itemSocores);
        initGame();
    }
    
    public void setPlayerName(String name) {
        App.getParticipateFrame().playerName = name;
        this.lbPlayer.setText(App.getParticipateFrame().playerName);
    }

    private boolean  insertnewSession() {
        try {
            DB.getConnection().setAutoCommit(false);
            PreparedStatement sm = DB.getConnection().prepareStatement("INSERT INTO Sessions (SessionID, PlayerID, StartDate) VALUES (?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
            sessionID = String.valueOf(new Date().getTime()).toString();
            sm.setString(1, sessionID);
            sm.setInt(2, App.userID);
            sm.setDate(3, Utils.getCurrentJavaSqlDate());

            sm.executeUpdate();
            DB.getConnection().commit();
            return true;
            
        } catch (SQLException ex) {
            try {
                DB.getConnection().rollback();
                DB.getConnection().setAutoCommit(true);

            } catch (SQLException ex1) {
                Logger.getLogger(Play.class.getName()).log(Level.SEVERE, null, ex1);
            }
            Logger.getLogger(Play.class.getName()).log(Level.SEVERE, null, ex);
        }

        return false;

    }
    
     private void initGame() {
        //Insert new player session
        if(insertnewSession()) {
        this.lbMoney.setText("0");
        listSelectedIndex = 0;
        this.lbPlayer.setText(App.getParticipateFrame().playerName);
        this.setListSelectIndex(listSelectedIndex);
        this.getQuestion();

        //enable cheats button
        this.btn50.setEnabled(true);
        fifty = false;
        this.btnFlip.setEnabled(true);
        flip = false;
        this.btnDouble.setEnabled(true);
        doubledip = false;

        //Enable Walkaway button
        this.btnWalkAway.setEnabled(true);

        //Clear doubledip selected answer
        AnswerSelectList.clear();
        }
        else {
            Utils.Alert("System busy now.");
        }
    }
     
     private void setListSelectIndex(int index) {
        this.PrizeMoney.setSelectedIndex(index);
    }
     
     private void getQuestion() {
        if (listSelectedIndex == 11) {
            this.btnWalkAway.setEnabled(false);
        }
        ResultSet rs = DB.execQuery("Select top 1 * from Questions order by NEWID()");
        try {
            if (rs.next()) {
                this.jQuestion.setText(rs.getString("name"));
                this.getAnswer(rs.getInt("QuestionID"));
                rs.close();
            } else {
                Utils.Alert("Sorry no question in our database now!");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            Utils.Alert(ex.getMessage());
        }

        //Enable default for all answer button
        this.btn1.setEnabled(true);
        this.btn2.setEnabled(true);
        this.btn3.setEnabled(true);
        this.btn4.setEnabled(true);
    }
     
     private void getAnswer(int questionID) {
        this.btnNext.setVisible(false);
        ResultSet rs = DB.execQuery("Select top 4 * from Answers where QuestionID='" + questionID + "' order by NEWID()");
        try {
            if (rs.next()) {
                this.btn1.setText(rs.getString("Contents"));
                if (rs.getBoolean("isCorrect") == true) {
                    this.answer = rs.getString("Contents");
                    this.rightAnswerButtonIdx = 1;
                }
                rs.next();
                this.btn2.setText(rs.getString("Contents"));
                if (rs.getBoolean("isCorrect") == true) {
                    this.answer = rs.getString("Contents");
                    this.rightAnswerButtonIdx = 2;
                }
                rs.next();
                this.btn3.setText(rs.getString("Contents"));
                if (rs.getBoolean("isCorrect") == true) {
                    this.answer = rs.getString("Contents");
                    this.rightAnswerButtonIdx = 3;
                }
                rs.next();
                this.btn4.setText(rs.getString("Contents"));
                if (rs.getBoolean("isCorrect") == true) {
                    this.answer = rs.getString("Contents");
                    this.rightAnswerButtonIdx = 4;
                }

                rs.close();
            } else {
                Utils.Alert("Sorry no answers in our database now!");
            }
        } catch (SQLException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
            Utils.Alert(ex.getMessage());
        }
    }

    private void getSelectAnswer(JToggleButton btn, int btnIdx) {
        if (doubledip == false) {
            //Reset toogle button state
            btn.setSelected(false);

            //if (btn.getText().equals(this.answer)) {
            if (btnIdx == this.rightAnswerButtonIdx) {
                btn.setBackground(Color.green);
                this.btnNext.setVisible(true);
                lastButton = btn;
            } else {
                EndGame();
            }
        } else {
            if (AnswerSelectList.size() < 1) {
                AnswerSelectList.add(btn);
            } else {
                doubledip = false;
                AnswerSelectList.add(btn);
                boolean correctAnswer = false;
                for (int i = 0; i < AnswerSelectList.size(); i++) {
                    JToggleButton ma = JToggleButton.class.cast(AnswerSelectList.get(i));
                    ma.setSelected(false);
                    if (ma.getText().equals(this.answer)) {
                        ma.setBackground(Color.green);
                        this.btnNext.setVisible(true);
                        lastButton = ma;
                        correctAnswer = true;
                    }
                }

                if (correctAnswer == false) {
                    EndGame();
                }
            }
        }
    }
    
    private void handleNextQuestion() {

        lastButton.setBackground(Color.getHSBColor(0, 0, Float.valueOf("0.95")));
        if (listSelectedIndex == 12) {
            WinGame();
        }
        this.getQuestion();
        this.displayCurrentMoney();
        ++listSelectedIndex;
        this.setListSelectIndex(listSelectedIndex);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        lbPlayer = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jQuestion = new javax.swing.JTextPane();
        btn50 = new javax.swing.JButton();
        btnFlip = new javax.swing.JButton();
        btnDouble = new javax.swing.JButton();
        btn1 = new javax.swing.JToggleButton();
        btn2 = new javax.swing.JToggleButton();
        btn3 = new javax.swing.JToggleButton();
        btn4 = new javax.swing.JToggleButton();
        jLabel3 = new javax.swing.JLabel();
        lbMoney = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        PrizeMoney = new javax.swing.JList();
        btnWalkAway = new javax.swing.JButton();
        btnNext = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Play Frame");

        jLabel1.setText("Welcome");

        lbPlayer.setText("#name#");

        jQuestion.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jQuestion.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jQuestion.setForeground(new java.awt.Color(0, 255, 0));
        jQuestion.setDisabledTextColor(new java.awt.Color(0, 0, 255));
        jQuestion.setOpaque(false);
        jScrollPane1.setViewportView(jQuestion);

        btn50.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/50-50.png"))); // NOI18N
        btn50.setToolTipText("");
        btn50.setPreferredSize(new java.awt.Dimension(70, 40));
        btn50.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn50ActionPerformed(evt);
            }
        });

        btnFlip.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/flip.png"))); // NOI18N
        btnFlip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFlipActionPerformed(evt);
            }
        });

        btnDouble.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/doubledip.gif"))); // NOI18N
        btnDouble.setPreferredSize(new java.awt.Dimension(60, 40));
        btnDouble.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDoubleActionPerformed(evt);
            }
        });

        btn1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/1.png"))); // NOI18N
        btn1.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn1ActionPerformed(evt);
            }
        });

        btn2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/2.png"))); // NOI18N
        btn2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn2ActionPerformed(evt);
            }
        });

        btn3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/3.png"))); // NOI18N
        btn3.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn3ActionPerformed(evt);
            }
        });

        btn4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/4.png"))); // NOI18N
        btn4.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        btn4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn4ActionPerformed(evt);
            }
        });

        jLabel3.setText("Current Money :");

        lbMoney.setText("0 $");

        PrizeMoney.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(PrizeMoney);

        btnWalkAway.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/walk away.png"))); // NOI18N
        btnWalkAway.setMaximumSize(new java.awt.Dimension(60, 60));
        btnWalkAway.setMinimumSize(new java.awt.Dimension(60, 60));
        btnWalkAway.setPreferredSize(new java.awt.Dimension(60, 60));
        btnWalkAway.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWalkAwayActionPerformed(evt);
            }
        });

        btnNext.setIcon(new javax.swing.ImageIcon(getClass().getResource("/image/next.png"))); // NOI18N
        btnNext.setText("Next");
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnFlip, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnWalkAway, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lbPlayer)
                                .addGap(168, 168, 168)
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbMoney))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(4, 4, 4)
                                    .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btn4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(18, 18, 18)
                                    .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 76, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnDouble, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(btnNext))
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(lbPlayer))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel3)
                        .addComponent(lbMoney)))
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btn50, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnFlip)
                                .addGap(11, 11, 11)
                                .addComponent(btnDouble, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(btnWalkAway, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btn1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btn2, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnNext)
                    .addComponent(btn4, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btn3, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 18, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNextActionPerformed
        this.handleNextQuestion();
    }//GEN-LAST:event_btnNextActionPerformed

    private void btnWalkAwayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWalkAwayActionPerformed
        WalkAway();
    }//GEN-LAST:event_btnWalkAwayActionPerformed

    private void btn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn1ActionPerformed
        this.getSelectAnswer(this.btn1, 1);
    }//GEN-LAST:event_btn1ActionPerformed

    private void btn2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn2ActionPerformed
        this.getSelectAnswer(this.btn2, 2);
    }//GEN-LAST:event_btn2ActionPerformed

    private void btn3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn3ActionPerformed
        this.getSelectAnswer(this.btn3, 3);
    }//GEN-LAST:event_btn3ActionPerformed

    private void btn4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn4ActionPerformed
        this.getSelectAnswer(this.btn4, 4);
    }//GEN-LAST:event_btn4ActionPerformed

    private void btn50ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn50ActionPerformed
        this.btn50.setEnabled(false);
        fifty = true;

        //Now have to hidden 2 button have a wrong answer
        int a = this.rand(1, 4, rightAnswerButtonIdx);
        setDisableAnswerButton(a);
        setDisableAnswerButton(rand(1, 4, a));
    }//GEN-LAST:event_btn50ActionPerformed

    private void btnFlipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFlipActionPerformed
        this.btnFlip.setEnabled(false);
        flip = true;
        //now we have to change a new question without increase the question list index
        this.getQuestion();
    }//GEN-LAST:event_btnFlipActionPerformed
    
     private void setDisableAnswerButton(int BtnIdx) {
        switch (BtnIdx) {
            case 1:
                this.btn1.setEnabled(false);
                this.btn1.setText("");
                break;
            case 2:
                this.btn2.setEnabled(false);
                this.btn2.setText("");
                break;
            case 3:
                this.btn3.setEnabled(false);
                this.btn3.setText("");
                break;
            case 4:
                this.btn4.setEnabled(false);
                this.btn4.setText("");
                break;
        }
    }

    public int rand(int lo, int hi, int notIn) {
        int n = hi - lo + 1;
        int i = new Random().nextInt() % n;
        if (i < 0) {
            i = -i;
        }

        int result = lo + i;

        if (result == notIn || result == rightAnswerButtonIdx) {
            result = rand(lo, hi, notIn);
        }
        return result;
    }
    
    int getCurrentMoney() {
        ListPoint a = (ListPoint) this.pointList.get(listSelectedIndex);
        //this.money.setText(Integer.toString(a.point));
        App.playerCurrentPoint = a.point;
        return a.point;
    }

    private void displayCurrentMoney() {
        ListPoint a = (ListPoint) this.pointList.get(listSelectedIndex);
        this.lbMoney.setText(Integer.toString(a.point));
    }
    
    private void btnDoubleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDoubleActionPerformed
        this.btnDouble.setEnabled(false);
        doubledip = true;
        usedDoubledip = true;
    }//GEN-LAST:event_btnDoubleActionPerformed
    
    private void formWindowClosing(java.awt.event.WindowEvent evt) {                                   
        EndGame();
        //App.switchDisplay(App.getMainFrame());
    }                                  

    public void resetGame() {
        initGame();
    }

    private void EndGame() {
        ListPoint a = (ListPoint) this.pointList.get(listSelectedIndex);
        App.playerCurrentPoint = a.wrong;
        this.dispose();
        new Congratulation().setVisible(true);
    }
    
    private void WinGame() {
        ListPoint a = (ListPoint) this.pointList.get(listSelectedIndex);
        App.playerCurrentPoint = a.point;
        this.dispose();
        new Congratulation().setVisible(true);
    }

    private void WalkAway() {
        ListPoint a = (ListPoint) this.pointList.get(listSelectedIndex);
        App.playerCurrentPoint = a.walk;

        this.dispose();
        new Congratulation().setVisible(true);
    }
    
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Play.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Play.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Play.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Play.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Play().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList PrizeMoney;
    private javax.swing.JToggleButton btn1;
    private javax.swing.JToggleButton btn2;
    private javax.swing.JToggleButton btn3;
    private javax.swing.JToggleButton btn4;
    private javax.swing.JButton btn50;
    private javax.swing.JButton btnDouble;
    private javax.swing.JButton btnFlip;
    private javax.swing.JButton btnNext;
    private javax.swing.JButton btnWalkAway;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextPane jQuestion;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lbMoney;
    private javax.swing.JLabel lbPlayer;
    // End of variables declaration//GEN-END:variables
}
